#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>

int main()
{
    //printf("printf writed\n");
    //fprintf(stdout, "fprintf writed\n");
    //char s1[] = "fwrite writed\n";
    //fwrite(s1, strlen(s1), 1, stdout);
    //char s2[] = "fputs writed\n";
    //fputs(s2, stdout);
    //char s3[] = "write writed\n";
    //write(1, s3, strlen(s3));
    ////printf("\n");
    //fork();

    int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    fprintf(stdout, "stdout writed\n");
    dup2(fd, 1);        //stdout类型不是int
    fprintf(stdout, "fprintf writed\n");
    char s[] = "fwrite writed\n";
    fwrite(s, strlen(s), 1, stdout);
    close(fd);
    fprintf(stdout, "code end\n");

    //close(1);
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);       //宏参数之间需要用|隔开，而不是用,隔开
    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}
    ////fprintf(stdout, "fprintf\n");
    ////char s[20];
    ////fgets(s, sizeof s, stdin);
    ////printf("%s\n", s);

    //printf("snowdragon writed\n");
    //printf("%d\n", fd);
    //printf("%d\n", fd);
    //printf("%d\n", fd);
    ////fprintf(stdout, "fprintf writed\n");
    ////char s[] = "fwrite writed\n";
    ////fwrite(s, strlen(s), 1, stdout);
    //fflush(stdout);     //先执行此语句再执行下条语句才能刷新，否则，不执行此语句直接执行下条语句将不会刷新
    //close(fd);        //不执行这两条语句，进程正常退出将会刷新

    return 0;
}
